package com.xsy.android.compose.wandroid.ui.page.login

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.xsy.android.compose.wandroid.Play
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.BaseModel
import com.xsy.android.compose.wandroid.logic.model.LoginModel
import com.xsy.android.compose.wandroid.logic.model.PlayError
import com.xsy.android.compose.wandroid.logic.model.PlayLoading
import com.xsy.android.compose.wandroid.logic.model.PlayState
import com.xsy.android.compose.wandroid.logic.model.PlaySuccess
import com.xsy.android.compose.wandroid.logic.utils.showToast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@author: xiaosy
 *时间: 2024/7/4
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class LoginViewModel(app: Application) : AndroidViewModel(app) {
    private val repository: AccountRepository = AccountRepository()
    private val _state = MutableLiveData<PlayState<LoginModel>>()
    val state: LiveData<PlayState<LoginModel>>
        get() = _state

    fun toLoginOrRegister(account: Account) {
        _state.postValue(PlayLoading)
        viewModelScope.launch(Dispatchers.IO) {
            val loginModel: BaseModel<LoginModel> = if (account.isLogin) {
                repository.getLogin(account.username, account.password)
            } else {
                repository.getRegister(
                    account.username,
                    account.password,
                    account.password
                )
            }
            if (loginModel.errorCode == 0) {
                val login = loginModel.data
                _state.postValue(PlaySuccess(login))
                Play.isLogin = true
                Play.setUserInfo(login.nickname, login.username)
                withContext(Dispatchers.Main) {
                    showToast(
                        getApplication(),
                        if (account.isLogin)
                            getApplication<Application>().getString(R.string.login_success)
                        else getApplication<Application>().getString(
                            R.string.register_success
                        )
                    )
                }

            } else {
                withContext(Dispatchers.Main) {
                    showToast(getApplication(), loginModel.errorMsg)
                }
                _state.postValue(PlayError(IllegalArgumentException("网络错误")))
            }
        }
    }

    private val _logoutState = MutableLiveData<LogoutSate>()
    val logoutSate: LiveData<LogoutSate>
        get() = _logoutState

    fun logout() {
        viewModelScope.launch {
            repository.getLogout()
            Play.logout()
            _logoutState.postValue(LogoutFinish)
        }
    }
}

sealed class LogoutSate
data object LogoutFinish : LogoutSate()
data object LogoutDefault : LogoutSate()

data class Account(val username: String, val password: String, val isLogin: Boolean)